<?php

/**
 * search.php
 *
 * Author: netman
 * DateTime:  2023/10/18 09:37
 */
class Service_Api_Search extends Contract_Api_Search
{
    use Trait_Api_Common;
    use Trait_Api_Search;

    function search_config($params): array
    {
        $pinyin = $params['pinyin'];//模块拼音
        $startcity = $params['startcity'] ?? '';
        $keyword = $params['keyword'] ?? '';
        $searchcode = empty($keyword) ? '' : St_String::split_keyword($keyword);//分词

        if ($pinyin) {
            $type_id = DB::select('id')->from('model')->where('pinyin', '=', $pinyin)->execute()->get('id');
        }
        $searchmodel = $this->get_search_model($searchcode, $keyword, $type_id, $startcity);

        // 所有的分类
        foreach ($searchmodel['all_search_model'] as &$v1) {
            $v1 = array(
                'typeid' => $v1['typeid'],
                'shortname' => $v1['shortname'],
                'pinyin' => $v1['pinyin'],
                'id' => $v1['id'],
            );
        }
        // 有产品的分类
        foreach ($searchmodel['has_search'] as &$v2) {
            $v2 = array(
                'typeid' => $v2['typeid'],
                'shortname' => $v2['shortname'],
                'pinyin' => $v2['pinyin'],
                'id' => $v2['id'],
            );
        }
        $result = array(
            'all_search_model' => $searchmodel['all_search_model'],
            'has_search' => $searchmodel['has_search']
        );
        return $result;
    }

    function search_more($params): array
    {
        $page = $params['page'];
        $pagesize = $params['pagesize'];
        $keyword = $params['keyword'] ?? '';
        $searchcode = empty($keyword) ? '' : St_String::split_keyword($keyword);//分词
        $startcity = $params['startcity'] ?? '';
        $pinyin = $params['pinyin'];//模块拼音
        $platform_no= $params['platform_no'];
        $type_id = '';
        if ($pinyin) {
            $type_id = DB::select('id')->from('model')->where('pinyin', '=', $pinyin)->execute()->get('id');
        }
        if ( ! $type_id) {
            throw new Exception("pinyin 参数有误");
        }

        $route_array = array(
            'pinyin' => $pinyin,
            'keyword' => $keyword,
            'typeid' => $type_id,
            'startcity' => $startcity
        );

        $out = Model_Global_Search::search_result($route_array, $searchcode, $page, $pagesize,$platform_no);
        if(count($out['list'])>0){
            $uni_img = Model_Uniapp_Images::get_img_list($type_id);
        }
        foreach ($out['list'] as &$l) {
            if(isset($uni_img[$l['id']])){
                $l['litpic'] = $uni_img[$l['id']];
            }
            $l = array(
                'id' => $l['id'],
                'litpic' => $this->img_url($l['litpic'],750,750),
                'addtime' => date('Y-m-d H:i:s', $l['addtime']),
                'title' => $l['title'],
                'attrlist' => $l['attrlist'] ? $l['attrlist'] : array(),
                'sellpoint' => $l['sellpoint'] ? $l['sellpoint'] : '',
                'address' => $l['address'] ? $l['sellpoint'] : '',
                'price' => $l['price'] ? $l['price'] : 0,
                'shownum' => $l['shownum'] ? $l['shownum'] : 0,
                'sellnum' => $l['sellnum'] ? $l['sellnum'] : 0,
                'commentnum' => $l['commentnum'] ? $l['commentnum'] : 0,
                'abstract' => $l['abstract'] ? $l['abstract'] : '',
                'summary' => $l['summary'] ? $l['summary'] : '',
                'satisfyscore' => $l['satisfyscore'] ? $l['satisfyscore'] : 100,
            );
        }
        if (count($out['list']) == $pagesize) {
            $page++;
        } else {
            $page = -1;
        }
        $out['page'] = $page;
        return $out;
    }


}